package com.example.demo.services.Impl;

import com.example.demo.entities.User1;
import com.example.demo.entities.User2;
import com.example.demo.mapper.UserMapper;
import com.example.demo.repository.UserDao;
import com.example.demo.entities.User;
import com.example.demo.repository.UserRepository;
import com.example.demo.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Scope("prototype")
public class UserServiceImpl implements UserService {

    private final UserDao userDao;
    private  final UserRepository userRepository;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserDao userDao,
                           UserRepository userRepository) {
        this.userDao = userDao;
        this.userRepository = userRepository;
    }

    @Override
    public List<User> queryUsers() {
        return userDao.queryUsers();
    }

    @Override
    public User getUser(long id) {
        return  userDao.getUser(id);
    }

    @Override
    public List<User1> getUser(String name) {
        return userRepository.findAllByUsername(name);
    }

    @Override
    public boolean delete(long id) {
        try {
            userRepository.deleteById(id);
            return  true;
        }catch (Exception e){
            return  false;
        }
    }

    @Override
    public boolean add(User1 user1) {
        try {
            userRepository.save(user1);
            return  true;
        }catch (Exception e){
            return  false;
        }
    }

    @Override
    public List<User2> findByUsername(String name) {
        return userMapper.findByUsername(name);
    }
}
